package com.cyx.controller;

import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @RequestMapping("/user/login")
    public String loing(@RequestParam("userName") String userName,
                        @RequestParam("passWord") String passWord,
                        Model model, HttpSession session){
        if(!ObjectUtils.isEmpty(userName)&& "123".equals(passWord)){
            session.setAttribute("loginUser",userName);
            return "redirect:/main";
        }else{
            model.addAttribute("msg","用户名或密码错误");
            return "index";
        }

    }

    @GetMapping("/main")
    public String main(){
        return "dashboard";
    }


    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "index";
    }
}
